package com.app.service;

import com.app.model.aspect.OperatorLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Lichaojie on 2017/8/10. It's a beautiful day.
 */
@Service
public class AspectService {

    public void saveLog(OperatorLog operatorLog){
        Map<String,Object> map = new HashMap<>();

        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(operatorLog.getRequestTime());

        map.put("beanName",operatorLog.getBeanName());
        map.put("curUser",operatorLog.getCurUser());
        map.put("methodName",operatorLog.getMethodName());
        map.put("params",operatorLog.getParams());
        map.put("remoteAddr",operatorLog.getRemoteAddr());
        map.put("sessionId",operatorLog.getSessionId());
        map.put("uri",operatorLog.getUri());
        map.put("requestTime",calendar.getTime());
        map.put("result", operatorLog.getResult());
        map.put("duration", operatorLog.getDuration());

        try{
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("../campus.logs/record.log",true));
            objectOutputStream.writeObject(map.toString() + "\n");
            objectOutputStream.close();
        }catch (IOException ioe){
            ioe.printStackTrace();
        }

    }
}
